package ligne2vie.topos.client;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * The class {@link SimpleMessageBox} allows displaying a common {@link DialogBox} with an HTML message and a close
 * button. No other business will be included here.
 * 
 * @author thuguerre
 * @since 1.0.0
 */
public class SimpleMessageBox {

	/**
	 * Default constructor. Display and center the {@link DialogBox}.
	 * 
	 * @param title
	 *            Title of the dialog box. This is not an HTML text.
	 * @param message
	 *            HTML message (be careful about accent) of the dialog box.
	 */
	public SimpleMessageBox(String title, String message) {

		// --- creating the dialog box
		final DialogBox dialogBox = new DialogBox();
		dialogBox.setText(title);

		// --- creating the OK button
		final Button okButton = new Button("OK");
		okButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				dialogBox.hide();
				okButton.setEnabled(true);
				okButton.setFocus(true);
			}
		});

		// --- creating the central panel, adding message and close button
		VerticalPanel centralPanel = new VerticalPanel();
		centralPanel.addStyleName("dialogVPanel");
		centralPanel.add(new HTML(message));
		centralPanel.setHorizontalAlignment(VerticalPanel.ALIGN_CENTER);
		centralPanel.add(okButton);

		dialogBox.setWidget(centralPanel);

		// --- displaying the dialog box with animation
		dialogBox.setAnimationEnabled(true);
		dialogBox.setGlassEnabled(true);
		dialogBox.center();

		// setting the focus on the button here allows user to directly hit Enter key
		okButton.setFocus(true);
	}
}
